var type;
var serviceObj;
var appointDay;

var account = $api.getStorage("account");

apiready = function(){
	type = api.pageParam.type;
	
	$api.text($api.byId('title'), '预约'+type);
	
	serviceObj = api.pageParam.serviceObj;
	
	$api.text($api.byId('lowprice'), serviceObj.lowprice||'0');
	$api.text($api.byId('info'), '服务简介：'+serviceObj.info);
	
	var date = new Date();   //今天
    var date2 = new Date(date.getTime() + 24*60*60*1000);  //明天
    var date3 = new Date(date.getTime() + (24*60*60*1000)*2);  //后天
    var date4 = new Date(date.getTime() + (24*60*60*1000)*3);  //大后天
    
    var month1=date.getMonth()+1;  
    var month2=date2.getMonth()+1;  
    var month3=date3.getMonth()+1;  
    var month4=date4.getMonth()+1; 
    
    var day1=date.getDate();
    var day2=date2.getDate();
    var day3=date3.getDate();
    var day4=date4.getDate();
    
    var week1=new Array("周日", "周一", "周二", "周三", "周四", "周五", "周六")[date.getDay()];
    var week2=new Array("周日", "周一", "周二", "周三", "周四", "周五", "周六")[date2.getDay()];
    var week3=new Array("周日", "周一", "周二", "周三", "周四", "周五", "周六")[date3.getDay()];
    var week4=new Array("周日", "周一", "周二", "周三", "周四", "周五", "周六")[date4.getDay()];
    
    $api.text($api.byId('day1'), month1+'月'+day1+'号');
    $api.val($api.byId('day1'), month1+'月'+day1+'号');
    $api.text($api.byId('week1'), week1);
    $api.text($api.byId('day1_text'), month1+'月'+day1+'号');
    
    $api.text($api.byId('day2'), month2+'月'+day2+'号');
    $api.val($api.byId('day2'), month2+'月'+day2+'号');
    $api.text($api.byId('week2'), week2);
    $api.text($api.byId('day2_text'), month2+'月'+day2+'号');
    
    $api.text($api.byId('day3'), month3+'月'+day3+'号');
    $api.val($api.byId('day3'), month3+'月'+day3+'号');
    $api.text($api.byId('week3'), week3);
    $api.text($api.byId('day3_text'), month3+'月'+day3+'号');
    
    $api.text($api.byId('day4'), month4+'月'+day4+'号');
    $api.val($api.byId('day4'), month4+'月'+day4+'号');
    $api.text($api.byId('week4'), week4);
    $api.text($api.byId('day4_text'), month4+'月'+day4+'号');
};


function enterExplain(){
    
	openWin('lifeservice_explain', {'explain':serviceObj.explain});
}


//提交预约
function clickSubmit(){

	var radio = document.getElementsByName("appointDay");
	var appointDay = "";
	
	for (var i = 0; i < radio.length; i++) {
		if (radio[i].checked) {
			appointDay = radio[i].value;
		}
	}
		
	var appointTime=$api.val($api.byId('appointTime'));
	var telephone=$api.val($api.byId('telephone'));
	 
	var userNo=$api.val($api.byId('userNo'));
	var number=$api.val($api.byId('number'));

	if(appointDay==''){

		toast('请选日期!');
		return;
	}
	if(telephone==''){

		toast('请输入手机号码!');
		return;
	}
	if(userNo==''){

		toast('请输入用户编号!');
		return;
	}
	if(number==''){

		toast('请输入数量!');
		return;
	}
	
	var data = {
		'account':account,
		'title':'预约'+type,
		'appointTime':appointDay+'  '+appointTime,
		'telephone':telephone,
		'merchantname':serviceObj.name,
		'category':type,
		'price':serviceObj.lowprice==''?'0':serviceObj.lowprice,
		'compounds':serviceObj.compounds,
		'userNo':userNo,
		'number':number,
		'wxopenId':serviceObj.wxopenId,
	};
	console.log(JSON.stringify(data));
	
	do_ajax('/app/community/addyuyue','post',data,function(result,err){

		if(result.resultCode == '0') {

			setTimeout(function () {
				api.closeWin();
            }, 2000);
             
			toast('预约成功');
             
		}else{
			toast(result.datas);
		}
		 
        api.hideProgress();
		    
	});
}